package tum0r.json;

import tum0r.utils.GetFields;

import java.lang.reflect.Field;
import java.util.*;

/**
 * 工程: XJSON<br>
 * 包: tum0r.json<br>
 * 创建者: tum0r<br>
 * 创建时间: 2020/9/20 11:11<br>
 * <br>
 */
public class JSONWriter {
    public static String serialize(Object data) {
        StringBuilder result = new StringBuilder();
        if (data != null) {
            if (data instanceof Byte ||
                    data instanceof Short ||
                    data instanceof Integer ||
                    data instanceof Long ||
                    data instanceof Float ||
                    data instanceof Double ||
                    data instanceof Boolean ||
                    data instanceof Character ||
                    data instanceof String ||
                    data instanceof JSON) {
                result.append(data.toString());
            } else if (data instanceof Collection || data.getClass().isArray()) {
                result.append(JSONArray.parse(data));
            } else if (data instanceof Map || data instanceof Dictionary || data instanceof Enumeration) {
                result.append(JSONObject.parse(data));
            } else {
                GetFields getFields = new GetFields();
                LinkedHashMap<Field, Object> fieldsValue = getFields.getFieldsValue(data, getFields.getFields(data));
                JSONObject temp = new JSONObject();
                for (Field key : fieldsValue.keySet()) {
                    Object value = fieldsValue.get(key);
                    if (value != null) {
                        if (value instanceof Byte ||
                                value instanceof Short ||
                                value instanceof Integer ||
                                value instanceof Long ||
                                value instanceof Float ||
                                value instanceof Double ||
                                value instanceof Boolean ||
                                value instanceof Character ||
                                value instanceof String ||
                                value instanceof JSON) {
                            temp.put(key.getName(), value);
                        } else if (value instanceof Date) {
                            temp.put(key.getName(), ((Date) value).getTime());
                        } else {
                            String serializeString = JSONWriter.serialize(value);
                            if (value instanceof Collection || value.getClass().isArray()) {
                                temp.put(key.getName(), JSONArray.parse(serializeString));
                            } else if (value instanceof Map || value instanceof Dictionary || value instanceof Enumeration) {
                                temp.put(key.getName(), JSONObject.parse(serializeString));
                            } else {
                                temp.put(key.getName(), serializeString);
                            }
                        }
                    } else {
                        temp.put(key.getName(), null);
                    }
                }
                result.append(temp.toString());
            }
        } else {
            result.append("null");
        }
        return result.toString();
    }
}
